package com.bnz.controller;

import com.bnz.entity.Classes;
import com.bnz.entity.PageResult;
import com.bnz.entity.Student;
import com.bnz.entity.StudentVo;
import com.bnz.service.ClassesService;
import com.bnz.service.StudentService;
import jdk.internal.dynalink.support.ClassMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassesService classesService;
    //查询所有
    @GetMapping("/list")
    public String findAll(Model model){
        List<Student> students = studentService.findAll();
//       放入Model
        model.addAttribute("students",students);

        return "student/list";
    }
    //分页
    @GetMapping("/findByPage")
    public String findByPage( @RequestParam(defaultValue = "1",required = false) Integer page,
                              @RequestParam(defaultValue = "3",required = false) Integer pageSize,
                              Model model){
        PageResult<Student> pr = studentService.findByPage(page, pageSize);
//        放入model
        model.addAttribute("pr",pr);
        //返回
        List<Classes> classes = classesService.findAll();
        model.addAttribute("classes",classes);
        return "student/list";
    }
    //分页
    //条件分页
    @RequestMapping("/search")
    public String search(     @RequestParam(defaultValue = "1",required = false) Integer page,
                              @RequestParam(defaultValue = "3",required = false) Integer pageSize,
                              StudentVo vo,
                              Model model){
        PageResult<Student> pr = studentService.search(page, pageSize,vo);
//        放入model
        model.addAttribute("pr",pr);
        //返回
        List<Classes> classes = classesService.findAll();
        model.addAttribute("classes",classes);
        return "student/list";
    }
    //增加
    @RequestMapping("/add")
    public String add( Student student){
        studentService.add(student);
        //
        return "student/list";
    }
}
